/****************************************************************************************
* This program reads in the raw schedule B files 
****************************************************************************************/

%macro pullschedb(yyyy);
	
	proc import out = schb_&yyyy
		datafile = "/.../F5500/f5500/F_SCH_B_&yyyy..csv" 
		dbms= csv replace;
		getnames = yes;
		datarow = /*redacted*/;
	run; 

	/*Do some re-naming and cleaning*/

	data blue.schb_&yyyy (keep= opr_ein opr_pn filing_id_char actrl_curr_value_ast_01_amt actrl_rpa94_info_curr_liab_amt 
	     actrl_liab_act_total_bnft_amt actrl_liab_rtd_total_bnft_amt actrl_invst_return_prcnt
	     actrl_cost_method_code actrl_rpa94_expt_incr_liab_amt actrl_weighted_rtm_age 
	     actrl_liab_act_partcp_cnt actrl_liab_rtd_partcp_cnt actrl_liab_term_partcp_cnt plan_year_end
	     rename = (actrl_curr_value_ast_01_amt = plan_assets
		       actrl_rpa94_info_curr_liab_amt = plan_liab
		       actrl_liab_act_total_bnft_amt = actives_liab
		       actrl_liab_rtd_total_bnft_amt = rtd_liab
		       actrl_invst_return_prcnt = asset_return
		       actrl_cost_method_code = cost_method_code
		       actrl_rpa94_expt_incr_liab_amt = xpct_liab_incr
		       actrl_weighted_rtm_age = plan_R_age
		       actrl_liab_act_partcp_cnt = actives_cnt
		       actrl_liab_rtd_partcp_cnt = rtd_cnt
		       actrl_liab_term_partcp_cnt = term_cnt));
		set schb_&yyyy;
		form_year=&yyyy;
		filing_id_char = /*redacted*/;
		opr_ein = left(trim(sch_b_ein));				/*rename for consistency with research file*/
		opr_pn = left(trim(sch_b_pn));					/*rename for consistency with research file*/
		len_pn = length(opr_pn);					/*length of pn field*/
		if len_pn=/*redacted*/ then opr_pn = /*redacted*/;
		else if len_pn=/*redacted*/ then opr_pn = /*redacted*/;
		
		%if &yyyy. <  2002 %then %do;
		    sch_b_tax_prd_c = /*redacted*/; 
		    ftp_yr = /*redacted*/; 				/*plan end year*/
		    ftp_mth = /*redacted*/; 				/*plan end month*/
		    ftp_day = /*redacted*/;				/*plan end day*/
		    ftp_date = mdy(ftp_mth,ftp_day,ftp_yr);					/*plan end sas date*/
		    plan_qtr_end = qtr(ftp_date);						/*plan calendar end qtr*/
		    plan_year_end = year(ftp_date);						/*plan calendar end year*/		
		%end;
		
		%else %do;
		    sch_b_tax_prd = left(trim(sch_b_tax_prd)); 
		    ftp_yr = /*redacted*/; 				/*plan end year*/
		    ftp_mth = /*redacted*/; 				/*plan end month*/
		    ftp_day = /*redacted*/;				/*plan end day*/
		    ftp_date = mdy(ftp_mth,ftp_day,ftp_yr);					/*plan end sas date*/
		    plan_qtr_end = qtr(ftp_date);						/*plan calendar end qtr*/
		    plan_year_end = year(ftp_date);						/*plan calendar end year*/		
		%end;
		
	run;

	/*Re-cast some variables from char to numeric*/

	data blue.schb_&yyyy (drop = plan_R_age
	      rename = (plan_R_age_n = plan_R_age));
	      set blue.schb_&yyyy;      
	      plan_R_age_n=/*redacted*/;
	run;

%mend pullschedb;


